package com.example.hospital.mapper;

import com.example.hospital.domain.entity.Admin;
import org.apache.ibatis.annotations.*;

import java.util.List;

@Mapper
public interface AdminMapper {
    @Insert("INSERT INTO admin (username, password, name, phone, create_time) VALUES (#{username}, #{password}, #{name}, #{phone}, NOW())")
    @Options(useGeneratedKeys = true, keyProperty = "adminId")
    int insert(Admin admin);

    @Select("SELECT * FROM admin WHERE username = #{username}")
    Admin findByUsername(@Param("username") String username);

    @Update("UPDATE admin SET name=#{name}, phone=#{phone} WHERE admin_id=#{adminId}")
    int updateProfile(Admin admin);

    @Select({
            "<script>",
            "SELECT * FROM admin",
            "<where>",
            "  <if test=\"name != null and name != ''\"> AND name LIKE CONCAT('%', #{name}, '%') </if>",
            "</where>",
            "ORDER BY create_time DESC",
            "LIMIT #{limit} OFFSET #{offset}",
            "</script>"
    })
    List<Admin> list(@Param("name") String name, @Param("limit") int limit, @Param("offset") int offset);

    @Select({
            "<script>",
            "SELECT COUNT(1) FROM admin",
            "<where>",
            "  <if test=\"name != null and name != ''\"> AND name LIKE CONCAT('%', #{name}, '%') </if>",
            "</where>",
            "</script>"
    })
    long count(@Param("name") String name);
}